package gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CustomJPanel extends JPanel{
	
	/**
	 * Class: CustomJPanel
	 * Description: highly customed beautiful JPanel >_>
	 * @category GUI
	 * @author Mr3chi
	 * @since 04/04/2013
	 *
	 */
	private static final long serialVersionUID = 6302467919989665522L;
	private JFrame parent;
	private int transparentIndex;
	private int cornerIndex;
	private Point sp;
	private Point p;
	private int	oldHeight;
	private int oldWidth;
	private boolean shouldMoveX;
	private boolean shouldMoveY;
	private int newHeight;
	private int newWidth;
	private int newLocX;
	private int newLocY;
	
	private int minofWidth = 200;
	private int minofHeight = 150;
	
	public CustomJPanel(JFrame param, int transparentIndex, int cornerIndex) {
		this.parent = param;
    	this.transparentIndex = transparentIndex;
    	this.cornerIndex = cornerIndex;
    	setOpaque(false);
    	addMouseListener(new CustomMouseAdapter());
    	addMouseMotionListener(new CustomMouseMotionAdapter());
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(new Color(0, 0, 0, transparentIndex));
        g.fillRoundRect(this.getX(), this.getY(), this.getWidth(), this.getHeight(), cornerIndex, cornerIndex);
        g.setColor(new Color(0, 0, 0, transparentIndex-10));
        g.clearRect(this.getX(), this.getY()+30, this.getWidth(), this.getHeight()-60);
        g.fillRect(this.getX(), this.getY()+30, this.getWidth(), this.getHeight()-60);
	}
	
	class CustomMouseAdapter extends MouseAdapter{
		/**
		 * (non-Javadoc)
		 * Class: CustomMouseAdapter
		 * Description: CustomJPanel's MouseAdapter
		 */
		@Override
		public void mouseExited(MouseEvent arg0) {
			if (sp == null){
				setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); 
			}
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			sp = arg0.getPoint(); 
			oldHeight = getSize().height;
			oldWidth = getSize().width;
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			sp = null;
		}
	}
	
	class CustomMouseMotionAdapter extends MouseMotionAdapter{
		/**
		 * (non-Javadoc)
		 * Class: CustomMouseMotionAdapter
		 * Description: CustomJPanel's MouseMotionAdapter
		 */
		@Override
		public void mouseDragged(MouseEvent arg0) {
			p = arg0.getPoint();
        	newHeight = getSize().height;
        	newWidth = getSize().width;
        	newLocX = parent.getLocation().x;
        	newLocY = parent.getLocation().y;
			//resize events
			int cursorType = getCursor().getType();
			if (cursorType == Cursor.SE_RESIZE_CURSOR){
				newHeight = oldHeight + p.y - sp.y;
				newWidth = oldWidth + p.x - sp.x;
			}else if(cursorType== Cursor.S_RESIZE_CURSOR){
				newHeight = oldHeight + p.y - sp.y;
			}else if(cursorType== Cursor.E_RESIZE_CURSOR){
				newWidth = oldWidth + p.x - sp.x;
			}else if(cursorType== Cursor.NW_RESIZE_CURSOR){
				newHeight = oldHeight - (p.y - sp.y);
				newWidth = oldWidth - (p.x - sp.x);
				newLocX += (p.x - sp.x);
				newLocY += (p.y - sp.y);
				oldWidth = newWidth;
				oldHeight = newHeight;
			}else if(cursorType== Cursor.N_RESIZE_CURSOR){
				newHeight = oldHeight - (p.y - sp.y);
				newLocY += (p.y - sp.y);
				oldHeight = newHeight;
			}else if(cursorType== Cursor.W_RESIZE_CURSOR){
				newWidth = oldWidth - (p.x - sp.x);
				newLocX += (p.x - sp.x);
				oldWidth = newWidth;
			}else if(cursorType== Cursor.NE_RESIZE_CURSOR){
				newHeight = oldHeight - (p.y - sp.y);
				newWidth = oldWidth + (p.x - sp.x);
				newLocY += (p.y - sp.y);
				oldHeight = newHeight;
			}else if(cursorType== Cursor.SW_RESIZE_CURSOR){
				newHeight = oldHeight + (p.y - sp.y);
				newWidth = oldWidth - (p.x - sp.x);
				newLocX += (p.x - sp.x);
				oldWidth = newWidth;
			}else if(cursorType == Cursor.HAND_CURSOR){
				newLocX += (p.x - sp.x);
				newLocY += (p.y - sp.y);
			}
			
			if(newWidth<minofWidth){
				newWidth = minofWidth;
				oldWidth = minofWidth;
				shouldMoveX = false;
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}else{
				shouldMoveX = true;
			}
			
			if(newHeight < minofHeight){
				newHeight = minofHeight;
				oldHeight = minofHeight;
				shouldMoveY = false;
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}else{
				shouldMoveY = true;
			}
			if(shouldMoveX&&shouldMoveY){
				parent.setLocation(newLocX, newLocY);
				parent.setSize(newWidth,newHeight);
			}else if(shouldMoveX){
				parent.setLocation(newLocX, parent.getLocation().y);
				parent.setSize(newWidth, parent.getSize().height);
			}else if(shouldMoveY){
				parent.setLocation(parent.getLocation().x, newLocY);
				parent.setSize(parent.getSize().width, newHeight);
			}else{
				parent.setLocation(parent.getLocation().x, parent.getLocation().y);
			}
		}

		@Override
		public void mouseMoved(MouseEvent arg0) {
			Point p = arg0.getPoint(); 
			int tempw = getSize().width;
			int temph = getSize().height;
			if (p.x>tempw-8 && p.y>temph-8){
				setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); 
			}else if(p.x>tempw-5){
				setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
			}else if(p.y>temph-5){
				setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
			}else if(p.x<8 && p.y<8){
				setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
			}else if(p.x<5){
				setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
			}else if(p.y<5){
				setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
			}else if(p.x>tempw-8 && p.y<8){
				setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
			}else if(p.x<8 && p.y>temph-8){
				setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
			}else {
				setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR)); 
			}
		}
		
	}
}

